using UnityEngine;
using QFramework;
using Unity.VisualScripting;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class CamareController : ViewController
	{

		[Header("震动持续时间")]
		float shakeDuration = 0.5f;

		[Header("震动强度")]
		[SerializeField]
		float shakeAmount = 0.1f;

		[Header("震动衰减速度")]
		[SerializeField]
		float decreaseFactor = 1.0f;

		private Vector2 originalPos;
		private float currentShakeDuration = 0f;
		private Vector2 positionTarget = Vector2.zero;

		public static CamareController Default;

		public static Transform LFtrans => Default.LF;
		public static Transform RItrans => Default.RI;

		void Awake()
		{
			Default = this;
		}

		void Start()
		{
			// Code Here
			// Application.targetFrameRate = 60;
		}

		void Update()
		{


			if (Player.Default)
			{
				positionTarget = Player.Default.transform.position;

				transform.PositionX((1.0f - Mathf.Exp(-Time.deltaTime * 10))
				.Lerp(transform.position.x, positionTarget.x));

				transform.PositionY((1.0f - Mathf.Exp(-Time.deltaTime * 10))
				.Lerp(transform.position.y, positionTarget.y));
				
				if (currentShakeDuration > 0)
				{
					float shakeX = originalPos.x + Random.insideUnitCircle.x * shakeAmount;
					float shakeY = originalPos.y + Random.insideUnitCircle.y * shakeAmount;

					//随机移动相机位置
					transform.position = new Vector3(shakeX,shakeY,transform.position.z);

					//减少震动持续时间
					currentShakeDuration -= Time.deltaTime * decreaseFactor;
				}
				else
				{
					currentShakeDuration = 0f;
					originalPos = positionTarget;
				}
			}
		}

		//相机抖动函数
		public void CamareShake()
		{
			currentShakeDuration = shakeDuration;
		}
	}
}
